package com.cwj.film.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cwj.common.Beans.film.requestBeans;
import com.cwj.common.domain.film;
import com.cwj.common.utils.utils;
import com.cwj.film.Listener.Beans;
import com.cwj.film.service.inter.upFilm;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class upFilmImpl implements upFilm {
    @Resource
    private com.cwj.film.dao.filmDao filmDao;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private ServletContext servletContext;
    @Resource
    private Beans beans;
    @Resource
    private HttpServletRequest request;


    @Override
    public boolean filmExist(film film) throws JsonProcessingException {



        Integer count = filmDao.selectCount(new QueryWrapper<film>().allEq(objectMapper.readValue(objectMapper.writeValueAsString(film), new TypeReference<Map<String,String>>(){})));

        return count > 0;

    }
    @Transactional
    @Order(1)
    @Override
    public boolean upFilm(film film, MultipartFile pict, MultipartFile[] films) throws IOException {
        List<File> fileList = ((requestBeans) request.getAttribute("requestBeans")).getFileList();

        filmDao.insert(film);
        String pictType = Objects.requireNonNull(pict.getOriginalFilename()).substring(pict.getOriginalFilename().lastIndexOf(".")+1);
        String pictName = film.getTitle() + "." + pictType;
        String upPictPath = servletContext.getRealPath("pict")+"\\UpPict\\"+pictName;
        File file = new File(upPictPath);

        pict.transferTo(file);
        fileList.add(file);
        List<Closeable>  closeableList =((requestBeans)request.getAttribute("requestBeans")).getCloseableList();

        utils.cut(upPictPath,beans.get_1740x1036path()+"\\"+pictName,1740,1036,closeableList);
        fileList.add(new File(beans.get_1740x1036path()+"\\"+pictName));
        utils.cut(upPictPath,beans.get_540x488path()+"\\"+pictName,540,488,closeableList);
        fileList.add(new File(beans.get_540x488path()+"\\"+pictName));
        utils.cut(upPictPath,beans.get_1140x1032path()+"\\"+pictName,1140,1032,closeableList);
        fileList.add(new File(beans.get_1140x1032path()+"\\"+pictName));

           if(new File(beans.getServerPath()+"film"+"\\"+film.getType()+"\\"+film.getTitle()).mkdirs()){
               fileList.add(new File(beans.getServerPath()+"film"+"\\"+film.getType()+"\\"+film.getTitle()));
           }

        for (MultipartFile multipartFile : films) {
            File f = new File(beans.getServerPath()+"film" +"\\"+ film.getType() + "\\" + film.getTitle() + "\\" + multipartFile.getOriginalFilename());
            multipartFile.transferTo(f);

        }
        return true;
    }

}
